home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StringTokenizer.java < prev    next >
Text File  |  1998-09-22  |  9KB  |  264 lines

  1. /*
  2.  * @(#)StringTokenizer.java    1.16 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. import java.lang.*;
  18.  
  19. /**
  20.  * The string tokenizer class allows an application to break a 
  21.  * string into tokens. The tokenization method is much simpler than 
  22.  * the one used by the <code>StreamTokenizer</code> class. The 
  23.  * <code>StringTokenizer</code> methods do not distinguish among 
  24.  * identifiers, numbers, and quoted strings, nor do they recognize 
  25.  * and skip comments. 
  26.  * <p>
  27.  * The set of delimiters (the characters that separate tokens) may 
  28.  * be specified either at creation time or on a per-token basis. 
  29.  * <p>
  30.  * An instance of <code>StringTokenizer</code> behaves in one of two 
  31.  * ways, depending on whether it was created with the 
  32.  * <code>returnTokens</code> flag having the value <code>true</code> 
  33.  * or <code>false</code>: 
  34.  * <ul>
  35.  * <li>If the flag is <code>false</code>, delimiter characters serve to 
  36.  *     separate tokens. A token is a maximal sequence of consecutive 
  37.  *     characters that are not delimiters. 
  38.  * <li>If the flag is <code>true</code>, delimiter characters are considered to
  39.  *     be tokens. A token is either one delimiter character, or a maximal
  40.  *     sequence of consecutive characters that are not delimiters.
  41.  * </ul>
  42.  * <p>
  43.  * The following is one example of the use of the tokenizer. The code:
  44.  * <blockquote><pre>
  45.  *     StringTokenizer st = new StringTokenizer("this is a test");
  46.  *     while (st.hasMoreTokens()) {
  47.  *         println(st.nextToken());
  48.  *     }
  49.  * </pre></blockquote>
  50.  * <p>
  51.  * prints the following output:
  52.  * <blockquote><pre>
  53.  *     this
  54.  *     is
  55.  *     a
  56.  *     test
  57.  * </pre></blockquote>
  58.  *
  59.  * @author  unascribed
  60.  * @version 1.16, 07/01/98
  61.  * @see     java.io.StreamTokenizer
  62.  * @since   JDK1.0
  63.  */
  64. public
  65. class StringTokenizer implements Enumeration {
  66.     private int currentPosition;
  67.     private int maxPosition;
  68.     private String str;
  69.     private String delimiters;
  70.     private boolean retTokens;
  71.  
  72.     /**
  73.      * Constructs a string tokenizer for the specified string. The 
  74.      * characters in the <code>delim</code> argument are the delimiters 
  75.      * for separating tokens. 
  76.      * <p>
  77.      * If the <code>returnTokens</code> flag is <code>true</code>, then 
  78.      * the delimiter characters are also returned as tokens. Each 
  79.      * delimiter is returned as a string of length one. If the flag is 
  80.      * <code>false</code>, the delimiter characters are skipped and only 
  81.      * serve as separators between tokens. 
  82.      *
  83.      * @param   str            a string to be parsed.
  84.      * @param   delim          the delimiters.
  85.      * @param   returnTokens   flag indicating whether to return the delimiters
  86.      *                         as tokens.
  87.      * @since   JDK1.0
  88.      */
  89.     public StringTokenizer(String str, String delim, boolean returnTokens) {
  90.     currentPosition = 0;
  91.     this.str = str;
  92.     maxPosition = str.length();
  93.     delimiters = delim;
  94.     retTokens = returnTokens;
  95.     }
  96.  
  97.     /**
  98.      * Constructs a string tokenizer for the specified string. The 
  99.      * characters in the <code>delim</code> argument are the delimiters 
  100.      * for separating tokens. 
  101.      *
  102.      * @param   str     a string to be parsed.
  103.      * @param   delim   the delimiters.
  104.      * @since   JDK1.0
  105.      */
  106.     public StringTokenizer(String str, String delim) {
  107.     this(str, delim, false);
  108.     }
  109.  
  110.     /**
  111.      * Constructs a string tokenizer for the specified string. The 
  112.      * tokenizer uses the default delimiter set, which is 
  113.      * <code>"\t\n\r"</code>: the space character, the tab
  114.      * character, the newline character, and the carriage-return character. 
  115.      *
  116.      * @param   str   a string to be parsed.
  117.      * @since   JDK1.0
  118.      */
  119.     public StringTokenizer(String str) {
  120.     this(str, " \t\n\r", false);
  121.     }
  122.  
  123.     /**
  124.      * Skips delimiters.
  125.      */
  126.     private void skipDelimiters() {
  127.     while (!retTokens &&
  128.            (currentPosition < maxPosition) &&
  129.            (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
  130.         currentPosition++;
  131.     }
  132.     }
  133.  
  134.     /**
  135.      * Tests if there are more tokens available from this tokenizer's string.
  136.      *
  137.      * @return  <code>true</code> if there are more tokens available from this
  138.      *          tokenizer's string; <code>false</code> otherwise.
  139.      * @since   JDK1.0
  140.      */
  141.     public boolean hasMoreTokens() {
  142.     skipDelimiters();
  143.     return (currentPosition < maxPosition);
  144.     }
  145.  
  146.     /**
  147.      * Returns the next token from this string tokenizer.
  148.      *
  149.      * @return     the next token from this string tokenizer.
  150.      * @exception  NoSuchElementException  if there are no more tokens in this
  151.      *               tokenizer's string.
  152.      * @since      JDK1.0
  153.      */
  154.     public String nextToken() {
  155.     skipDelimiters();
  156.  
  157.     if (currentPosition >= maxPosition) {
  158.         throw new NoSuchElementException();
  159.     }
  160.  
  161.     int start = currentPosition;
  162.     while ((currentPosition < maxPosition) && 
  163.            (delimiters.indexOf(str.charAt(currentPosition)) < 0)) {
  164.         currentPosition++;
  165.     }
  166.     if (retTokens && (start == currentPosition) &&
  167.         (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
  168.         currentPosition++;
  169.     }
  170.     return str.substring(start, currentPosition);
  171.     }
  172.  
  173.     /**
  174.      * Returns the next token in this string tokenizer's string. The new 
  175.      * delimiter set remains the default after this call. 
  176.      *
  177.      * @param      delim   the new delimiters.
  178.      * @return     the next token, after switching to the new delimiter set.
  179.      * @exception  NoSuchElementException  if there are no more tokens in this
  180.      *               tokenizer's string.
  181.      * @since   JDK1.0
  182.      */
  183.     public String nextToken(String delim) {
  184.     delimiters = delim;
  185.     return nextToken();
  186.     }
  187.  
  188.     /**
  189.      * Returns the same value as the <code>hasMoreTokens</code>
  190.      * method. It exists so that this class can implement the
  191.      * <code>Enumeration</code> interface. 
  192.      *
  193.      * @return  <code>true</code> if there are more tokens;
  194.      *          <code>false</code> otherwise.
  195.      * @see     java.util.Enumeration
  196.      * @see     java.util.StringTokenizer#hasMoreTokens()
  197.      * @since   JDK1.0
  198.      */
  199.     public boolean hasMoreElements() {
  200.     return hasMoreTokens();
  201.     }
  202.  
  203.     /**
  204.      * Returns the same value as the <code>nextToken</code> method,
  205.      * except that its declared return value is <code>Object</code> rather than
  206.      * <code>String</code>. It exists so that this class can implement the
  207.      * <code>Enumeration</code> interface. 
  208.      *
  209.      * @return     the next token in the string.
  210.      * @exception  NoSuchElementException  if there are no more tokens in this
  211.      *               tokenizer's string.
  212.      * @see        java.util.Enumeration
  213.      * @see        java.util.StringTokenizer#nextToken()
  214.      * @since      JDK1.0
  215.      */
  216.     public Object nextElement() {
  217.     return nextToken();
  218.     }
  219.  
  220.     /**
  221.      * Calculates the number of times that this tokenizer's 
  222.      * <code>nextToken</code> method can be called before it generates an 
  223.      * exception. 
  224.      *
  225.      * @return  the number of tokens remaining in the string using the current
  226.      *          delimiter set.
  227.      * @see     java.util.StringTokenizer#nextToken()
  228.      * @since   JDK1.0
  229.      */
  230.     public int countTokens() {
  231.     int count = 0;
  232.     int currpos = currentPosition;
  233.  
  234.     while (currpos < maxPosition) {
  235.         /*
  236.          * This is just skipDelimiters(); but it does not affect
  237.          * currentPosition.
  238.          */
  239.         while (!retTokens &&
  240.            (currpos < maxPosition) &&
  241.            (delimiters.indexOf(str.charAt(currpos)) >= 0)) {
  242.         currpos++;
  243.         }
  244.  
  245.         if (currpos >= maxPosition) {
  246.         break;
  247.         }
  248.  
  249.         int start = currpos;
  250.         while ((currpos < maxPosition) && 
  251.            (delimiters.indexOf(str.charAt(currpos)) < 0)) {
  252.         currpos++;
  253.         }
  254.         if (retTokens && (start == currpos) &&
  255.         (delimiters.indexOf(str.charAt(currpos)) >= 0)) {
  256.         currpos++;
  257.         }
  258.         count++;
  259.  
  260.     }
  261.     return count;
  262.     }
  263. }
  264.